<?php
namespace app\api\controller;
use app\common\controller\Api;

/**
 * 上传接口
**/
class Upload extends Api
{
    public function upload(): \think\response\Json
    {

        $file = $_FILES;
        if (empty($file)){
            return json(['data'=>'','msg'=>'提交方式不正确'],0);
        }
        $data = [];
        foreach ($file as $k => $value){
           $fileName = empty($value['name']) ? '' : $value['name'];
           $fileType = empty($value['type']) ? '' : $value['type'];
           $filePath = empty($value['tmp_name']) ? '' : $value['tmp_name'];
           $fileSize = empty($value['size']) ? '' : $value['size'];
           $suffix   = pathinfo($fileName,PATHINFO_EXTENSION);
           $objectName   = md5(time().$fileName).'.'.$suffix;
           $fileType = $fileType ? explode('/',$fileType)[0] : 'exampledir';
           $object   = $fileType.'/'.$objectName;
           $fileData = $this->oss_upload($filePath,$object);
//           $url     = empty($fileData['url']) ? '' : $fileData['url'];
           $success = empty($fileData['success']) ? '' : $fileData['success'];
           $data[$k]['url']       = $object;
           $data[$k]['success']   = $success;
           $data[$k]['type']   = $fileType;
           $data[$k]['size']   = $fileSize;
           (new \app\common\model\Resource)->insert([
               'name'=>$fileName,
               'type'=>$fileType,
               'size'=>round($fileSize/1024, 2),
               'suffix'=>$suffix,
               'url'=>$object,
               'success'=>$success,
               'ctime'=>date('Y-m-d H:i:s'),
           ]);
        }
        return json($data);
    }

}